//
//  StoreDetailViewController.m
//  BaseProject
//
//  Created by 王洋 on 2020/7/1.
//  Copyright © 2020 王洋. All rights reserved.
//
#import "StoreDetailRequest.h"

#import "StoreDetailModel.h"
#import "TIIMQuickReplyCardModel.h"

#import "StoreDetailInfoCell.h"
#import "StoreDetailTagCell.h"
#import "StoreDetailCouponCell.h"
#import "StoreDetailNormalActionCell.h"
#import "StoreDetailNormalShowCell.h"
#import "StoreDetailRingCell.h"

#import "StoreDetailAddressCell.h"
#import "StoreDetailPackageVCell.h"
#import "StoreDetailPackageCell.h"
#import "StoreDetailMerchantWorksCell.h"
#import "StoreDetailUserWorksCell.h"
#import "StoreDetailBusinessNewsCell.h"
#import "StoreDetailEvaluationCell.h"
#import "StoreDetailOpusCatCell.h"
#import "StoreQuickReplyView.h"
#import "IMBottomBubbleView.h"


#import "StoreDetailTableHeaderView.h"
#import "StoreDetailEvaluationHeader.h"
#import "StoreDetailNormalSectionHeader.h"
#import "StoreDetailNormalSectionFooter.h"

#import "StoreDetailBottomView.h"
#import "StoreDetailNaviView.h"

#import "WYXViewController.h"
#import "PackageListViewController.h"
//#import "PortfilioListViewController.h"
#import "PortfilioSegmentViewController.h"
#import "VideoStorySegmentViewController.h"
#import "BusinessMapNaviViewController.h"
#import "StoreDynamicListViewController.h"

#import "PortfilioDetailViewController.h"
#import "StoreDynamicDetailViewController.h"
#import "EvaluationDetailViewController.h"
#import "TT_ConversationDetailViewController.h"
#import "MessageCenterViewController.h"
#import "EvaluateListViewController.h"
#import "RingApplyViewController.h"
#import "ScheduleInquiryViewController.h"

#import "StoreDetailViewController.h"

@interface StoreDetailViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong)UITableView *tableview;

@property (nonatomic, strong) StoreDetailBottomView *bottomView;

@property (nonatomic, strong) StoreDetailTableHeaderView *headerView;

@property (nonatomic, strong) StoreDetailNaviView *naviView;

@property (nonatomic, strong) IMBottomBubbleView *bubbleView;

@property (nonatomic, strong) StoreDetailModel *storeDetailModel;

@property (nonatomic, assign) NSTimeInterval lastTime;

//快问
@property(nonatomic,strong)TIIMQuickReplyCardModel *quickReplyModel;
@property(nonatomic,strong)BaseAdjustButton *quickReplyButton;
@property(nonatomic,weak)StoreQuickReplyView *weakQuickReplyView;
@end

@implementation StoreDetailViewController

- (void)dealloc {
    NSLog(@"店铺释放了");
    [self handleStep];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //屏幕常亮
    [UIApplication sharedApplication].idleTimerDisabled = YES;
    self.navigationController.navigationBarHidden = YES;
    #if RT_INTERACTIVE_PUSH
        self.rt_navigationController.rt_enableInteractivePush = YES;
    #endif
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appBecomeActive) name:UIApplicationDidBecomeActiveNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleStep) name:UIApplicationDidEnterBackgroundNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleStep) name:UIApplicationWillTerminateNotification object:nil];
    self.lastTime = [Utilities getCurrentServerTimestamp];

    [self request_storeDetail];
}

- (void)appBecomeActive {
    self.lastTime = [Utilities getCurrentServerTimestamp];
}

- (void)handleStep {

    [[BA_BusinessHelper shared] addFootPrintWith:self.storeId lastTime:self.lastTime And:self.storeId type:BusinessCollectType_store];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}

- (void)initView {
    
    self.title = @"店铺详情";
    [self.naviView refreshNaviFavorStatusWith:self.storeDetailModel.isCollect];

    //屏幕常亮
    [UIApplication sharedApplication].idleTimerDisabled = YES;
    self.navigationController.navigationBarHidden = YES;
    #if RT_INTERACTIVE_PUSH
        self.rt_navigationController.rt_enableInteractivePush = YES;
    #endif
    
    if([self.tableview respondsToSelector:@selector(setContentInsetAdjustmentBehavior:)]) {
        if (@available(iOS 11.0, *)) {
            self.tableview.contentInsetAdjustmentBehavior=UIScrollViewContentInsetAdjustmentNever;
        } else {
            // Fallback on earlier versions
        }
    }
    
    WEAKSELF
    [RACObserve([TT_MessageManager manager], totalUnReadCount) subscribeNext:^(id x) {
        if ([TT_MessageManager manager].totalUnReadCount > 0) {
            weakSelf.naviView.msgBtn.badgeValue = [NSString stringWithFormat:@"%d",[TT_MessageManager manager].totalUnReadCount];
            weakSelf.naviView.msgBtn.badgeBGColor = [UIColor redColor];
        } else {
            weakSelf.naviView.msgBtn.badgeValue = @"";
            weakSelf.naviView.msgBtn.badgeBGColor = [UIColor clearColor];
        }
    }];
    
    [self.view addSubview:self.tableview];
    [self.view addSubview:self.bottomView];
    [self.view addSubview:self.naviView];

    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_equalTo(0);
        make.height.mas_equalTo(60 + Bottom_SafeHeight);
    }];
    
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.bottom.mas_equalTo(self.bottomView.mas_top).offset(-1);
    }];
    
    [self.naviView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.height.mas_equalTo(kStatusBarHeight + NavBar_Height);
    }];

    NSString *classId = self.storeDetailModel.seller.classId;
    if ([classId isEqualToString:Store_Category_siyi] ||
        [classId isEqualToString:Store_Category_genzhuang] ||
        [classId isEqualToString:Store_Category_sheying] ||
        [classId isEqualToString:Store_Category_genzhuang])  {
        [self.bottomView refreshWithType:StoreDetailBottomUIType_schedule];
    } else {
        
        [self.bottomView refreshWithType:StoreDetailBottomUIType_subscribe];
    }
    
    if (self.storeDetailModel.seller.consultingStar.length > 0) {

        CGFloat width = [Utilities getStringWidthWithString:self.storeDetailModel.seller.consultingStar fontSize:12 andHeight:36] + 70;
        self.bubbleView = [[IMBottomBubbleView alloc] initWithFrame:CGRectZero];
        [self.bubbleView.bubble setTitle:self.storeDetailModel.seller.consultingStar forState:UIControlStateNormal];
        
        [self.view addSubview:self.bubbleView];
        [self.bubbleView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(20);
            make.height.mas_equalTo(45);
            make.width.mas_equalTo(width);
            make.bottom.mas_equalTo(self.bottomView.mas_top);
        }];
        
        [self.view layoutIfNeeded];
    } else {
        self.bubbleView.hidden = YES;
    }
    [self.bottomView layoutIfNeeded];
    
    self.headerView =  [[NSBundle mainBundle] loadNibNamed:@"StoreDetailTableHeaderView"owner:nil options:nil].firstObject;
    self.headerView.frame = CGRectMake(0, 0, TTScreenWidth, [StoreDetailTableHeaderView calculateHeightWithModel:self.storeDetailModel]);

    self.tableview.tableHeaderView = self.headerView;
    self.headerView.autoresizingMask = UIViewAutoresizingNone;
    
    [self initViewAction];
    
    if ([TTUserModel sharedObject].userId.length > 0) {
        [[BA_BusinessHelper shared] browseStore:self.storeId
                                           type:BusinessActionType_tcs
                                       comption:^(BOOL success) {}];
    }
}

- (void)initViewAction {
        
    WEAKSELF
    self.bubbleView.IMBottomBubbleViewBlock = ^(BubbleActionType type) {
        switch (type) {
            case BubbleActionType_close:{
                [weakSelf.bubbleView removeFromSuperview];
            } break;
            case BubbleActionType_jump:{
                [[TT_IMContactManager manager] ImContactWithStoreId:weakSelf.storeId];
            } break;
            default:
                break;
        }
    };
    
    self.naviView.StoreDetailNaviViewBlock = ^(StoreDetailNaviViewActionType actionType) {
        
        switch (actionType) {
            case StoreDetailNaviViewActionType_back:{
                [weakSelf goBack];
            } break;
            case StoreDetailNaviViewActionType_favor:{
                
                [[BA_BusinessHelper shared] callCollectStoreWith:weakSelf.storeId
                                                   currentStatus:weakSelf.storeDetailModel.isCollect
                                                            type:BusinessCollectType_store
                                                        comption:^(BOOL success) {
                    
                    weakSelf.storeDetailModel.isCollect = !weakSelf.storeDetailModel.isCollect;
                    [weakSelf.naviView refreshNaviFavorStatusWith:weakSelf.storeDetailModel.isCollect];
                
                }];
                
            } break;
            case StoreDetailNaviViewActionType_share:{

                [weakSelf openLoginWithCompletion:^(BOOL isLogin, NSDictionary * _Nonnull options) {
                    if (isLogin) {
                        [weakSelf share];
                    }
                }];

            } break;
            case StoreDetailNaviViewActionType_im:{

                MessageCenterViewController *vc = [[MessageCenterViewController alloc] init];
                [weakSelf.navigationController pushViewController:vc animated:YES];
            } break;
            default:
                break;
        }
        
    };
    
    self.headerView.StoreDetailTableHeaderViewBlock = ^(StoreDetailTableHeaderType type, NSInteger index) {
      
        switch (type) {
            case StoreDetailTableHeaderType_wuyouxuan:{
                
                WYXViewController *vc = [[WYXViewController alloc] init];
                vc.storeId = weakSelf.storeDetailModel.seller.sellerId;
                [weakSelf.navigationController pushViewController:vc animated:YES];
                
            } break;
            case StoreDetailTableHeaderType_video:{
                
                VideoStorySegmentViewController *vc = [[VideoStorySegmentViewController alloc] init];
                vc.storeId = weakSelf.storeId;
                vc.model = weakSelf.storeDetailModel;
                vc.selectIndex = 0;
                [weakSelf.navigationController pushViewController:vc animated:YES];
                
            } break;
            case StoreDetailTableHeaderType_story:{
                
                VideoStorySegmentViewController *vc = [[VideoStorySegmentViewController alloc] init];
                vc.storeId = weakSelf.storeId;
                vc.model = weakSelf.storeDetailModel;
                vc.selectIndex = 1;
                [weakSelf.navigationController pushViewController:vc animated:YES];
                
            } break;
            case StoreDetailTableHeaderType_mapNavi:{
                
                BusinessMapNaviViewController *vc = [[BusinessMapNaviViewController alloc] init];
                vc.model = weakSelf.storeDetailModel.seller;
                [weakSelf.navigationController pushViewController:vc animated:YES];
                
            } break;
            case StoreDetailTableHeaderType_coupon:{

                [[BA_BusinessHelper shared] callgetCouponWith:weakSelf.storeDetailModel.sysCouponList[index].scoId And:weakSelf.storeId comption:^(BOOL success) {
                    if (success) {
                        [SVProgressHUD showSuccessWithStatus:@"领取成功"];
                        [SVProgressHUD dismissWithDelay:1];
                    }
                }];
                
            } break;
                
                
            default:
                break;
        }
    };
    
    self.bottomView.StoreDetailBottomBlock = ^(StoreDetailBottomActionType type) {
      
        switch (type) {
            case StoreDetailBottomActionType_im:{
                [[TT_IMContactManager manager] ImContactWithStoreId:weakSelf.storeId];
            } break;
            case StoreDetailBottomActionType_phone:{
                [[TT_PhonaCallManager manager] callWithStoreId:weakSelf.storeDetailModel.seller.sellerId];
            } break;
            case StoreDetailBottomActionType_schedule:{
                ScheduleInquiryViewController *vc = [[ScheduleInquiryViewController alloc] init];
                vc.sellerId = weakSelf.storeId;
                [weakSelf.navigationController pushViewController:vc animated:YES];
            } break;
            case StoreDetailBottomActionType_handle:{
                [[BA_BusinessHelper shared] callSubscribeStoreWith:weakSelf.storeId];
            } break;
            default:
                break;
        }
    };
}

#pragma mark Share
- (void)share {
    CommonShareModel *model = [[CommonShareModel alloc] init];
    model.url = [NSString stringWithFormat:@"%@%@?sellerId=%@&myCode=%@",BA_GlobalH5_Domain,@"/shopShare",self.storeId,[TTUserModel sharedObject].myCode];
    model.title = self.storeDetailModel.seller.sellerName;
    model.content = @"婚前婚后邀您体验";
    model.image = self.storeDetailModel.seller.logoUrl;
    if (self.storeDetailModel.seller.coverPaths.count > 0 && self.storeDetailModel.seller.logoUrl.length == 0) {
        model.image = self.storeDetailModel.seller.coverPaths.firstObject.url;
    }
    [[TT_ShareManager share] callShareViewWithModel:model];
}

- (void)goBack {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    if (_quickReplyButton) {
        WK(weakSelf)
        CGFloat right = WIDTH + (weakSelf.quickReplyButton.width - 24*ADAPTER_WIDTH);
        [UIView animateWithDuration:0.2 animations:^{
            weakSelf.quickReplyButton.right = right;
        }];
    }
    if (scrollView.tag !=10086) {
        return;
    }
    
    //    导航栏设置
    CGFloat height = (300 - kStatusBarHeight - NavBar_Height);
    if (scrollView.contentOffset.y > 1.1 * height) {
        return;
    }
    
    CGFloat alpha = scrollView.contentOffset.y/height;
    if (scrollView.contentOffset.y > 0.9 * height) {
        [self.naviView fillWithNaviStatus:StoreNaviViewStatusType_white isCollect:self.storeDetailModel.isCollect alpha:alpha];
    } else {
        [self.naviView fillWithNaviStatus:StoreNaviViewStatusType_clear isCollect:self.storeDetailModel.isCollect alpha:alpha];
    }
}

#pragma mark UITableviewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 6 + self.storeDetailModel.userEvaluateList.count;//这个1是评价，这个评价是动态的
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section <= StoreDetailSectionType_userReviews) {
        switch (section) {
            case StoreDetailSectionType_storeInfo:{
                return 4;
            } break;
            case StoreDetailSectionType_FeaturedPackages:{//精选套餐
                return 1;
            } break;
            case StoreDetailSectionType_groupPurchasePackage:{//团购套餐
                return self.storeDetailModel.sellingSellerGoodsList.count;
            } break;
            case StoreDetailSectionType_merchantWorks:{
                return 2;
            } break;
            case StoreDetailSectionType_businessNews:{
                return 1;
            } break;
            case StoreDetailSectionType_userReviews:{
                return 0;
            } break;
            default:
                break;
        }
    } else {
        return 1;
    }

    return 0;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section <=StoreDetailSectionType_userReviews) {
        
        switch (indexPath.section) {
            case StoreDetailSectionType_storeInfo:{

                switch (indexPath.row) {
                    case StoreDetailCellType_storeInfo:{
                        return 80;
                    } break;
                    case StoreDetailCellType_storeVideo:{
                        return TTScreenWidth *0.6;
                    } break;
                    case StoreDetailCellType_storeTag:{
                        return 60;
                    } break;
                    case StoreDetailCellType_storeAddress:{
                        return 60;
                    } break;
                    case StoreDetailCellType_storeCoupon:{
                        return 80;
                    } break;
                    case StoreDetailCellType_storeAnnouncement:{
                        
                        if (self.storeDetailModel.seller.sellerNotice.length > 0) {
                            return 60;
                        } else {
                            return CGFLOAT_MIN;
                        }
                    } break;
                    case StoreDetailCellType_storeConsultation:{
                        
                        if (self.storeDetailModel.seller.consultingStar.length > 0) {
                            return 60;
                        } else {
                            return CGFLOAT_MIN;
                        }
                    } break;
                    case StoreDetailCellType_storeArrival:{
                        
                        if (self.storeDetailModel.seller.arrivalGift.length > 0) {
                            return 60;
                        } else {
                            return CGFLOAT_MIN;
                        }
                    } break;
                    case StoreDetailCellType_subscribeRing:{
                        
                        if ([self.storeDetailModel.seller.classId isEqualToString:Store_Category_hunjie] && self.storeDetailModel.seller.sellerRecommendUrl.length > 0) {
                            return [StoreDetailRingCell calculateHeight];
                        } else {
                            return CGFLOAT_MIN;
                        }
                    } break;
                    default:
                        break;
                }

            } break;
            case StoreDetailSectionType_FeaturedPackages:{
                if (self.storeDetailModel.sellerGoodsList.count > 0) {
                    return [StoreDetailPackageVCell calculateHeight:self.storeDetailModel.sellerGoodsList];
                } else {
                    return CGFLOAT_MIN;
                }
            } break;
            case StoreDetailSectionType_groupPurchasePackage:{
                return 100;
            } break;
            case StoreDetailSectionType_merchantWorks:{
                switch (indexPath.row) {
                    case 0: {
                        if (self.storeDetailModel.sellerOpusClassificationList.count == 0) {
                            return CGFLOAT_MIN;
                        } else {
                            return 40;
                        }
                    } break;
                    case 1: {
                        if (self.storeDetailModel.sellerOpusList.count == 0) {
                            return CGFLOAT_MIN;
                        } else {
                            return [StoreDetailMerchantWorksCell calculateHeight];
                        }
                    } break;
                    default:
                        break;
                }
                

            } break;
            case StoreDetailSectionType_businessNews:{
                if (self.storeDetailModel.dynamicModel.sdyId.length > 0) {
                    if (self.storeDetailModel.dynamicModel.imageUrls.count > 0) {
                        return [StoreDetailBusinessNewsCell calculateHeight];
                    } else {
                        return 100;
                    }
                } else {
                    return CGFLOAT_MIN;
                }
            } break;
            case StoreDetailSectionType_userReviews:{//这个分组只作为头显示，真实评论走section
                return CGFLOAT_MIN;
            } break;
            default:
                break;
        }
        
    } else {
        return CGFLOAT_MIN;
    }
    return 80;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    if (section <= StoreDetailSectionType_userReviews) {
        switch (section) {
            case StoreDetailSectionType_storeInfo:{
                return CGFLOAT_MIN;
            } break;
            case StoreDetailSectionType_FeaturedPackages:{
                //精选套餐
                if (self.storeDetailModel.sellerGoodsList.count > 0) {
                    return 50;
                } else {
                    return CGFLOAT_MIN;
                }
            } break;
            case StoreDetailSectionType_groupPurchasePackage:{//热门套餐
                
                if (self.storeDetailModel.sellingSellerGoodsList.count > 0) {
                    return 50;
                } else {
                    return CGFLOAT_MIN;
                }
                
            } break;
            case StoreDetailSectionType_merchantWorks:{
                if (self.storeDetailModel.sellerOpusClassificationList.count == 0 && self.storeDetailModel.sellerOpusList.count == 0) {
                    return CGFLOAT_MIN;
                } else {
                    return 50;
                }

            } break;
            case StoreDetailSectionType_businessNews:{
                if (self.storeDetailModel.dynamicModel.sdyId.length > 0) {
                    return 50;
                } else {
                    return CGFLOAT_MIN;
                }
            } break;
            case StoreDetailSectionType_userReviews:{
                if (self.storeDetailModel.userEvaluateList.count > 0) {
                    return 50;
                } else {
                    return CGFLOAT_MIN;
                }
            } break;
            default:
                break;
        }
    } else {
        
        if (self.storeDetailModel.userEvaluateList.count > section - 6) {
            return self.storeDetailModel.userEvaluateList[section - 6].itemHeight;
//            return [StoreDetailEvaluationHeader calculateHeightWithModel:self.storeDetailModel.userEvaluateList[section - 6]];
        } else {
            return CGFLOAT_MIN;
        }

    }

    return 50;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    if (section <= StoreDetailSectionType_userReviews) {

        switch (section) {
            case StoreDetailSectionType_storeInfo:{
                return CGFLOAT_MIN;
            } break;
            case StoreDetailSectionType_FeaturedPackages:{
                return CGFLOAT_MIN;
            } break;
            case StoreDetailSectionType_groupPurchasePackage:{
                return CGFLOAT_MIN;
            } break;
            case StoreDetailSectionType_merchantWorks:{
                return CGFLOAT_MIN;
            } break;
            case StoreDetailSectionType_businessNews:{
                if (self.storeDetailModel.dynamicModel.sdyId.length > 0) {
                    return 50;
                } else {
                    return CGFLOAT_MIN;
                }
            } break;
            case StoreDetailSectionType_userReviews:{
                return CGFLOAT_MIN;
            } break;
            default:
                break;
        }
    } else {
        
    }
    return CGFLOAT_MIN;
}

#pragma mark sectionheader
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    if (section <= StoreDetailSectionType_userReviews) {
        
        StoreDetailNormalSectionHeader *headView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"StoreDetailNormalSectionHeader"];
        headView.contentView.backgroundColor = UIColor.whiteColor;
        headView.moreButton.hidden = YES;
        switch (section) {
            case StoreDetailSectionType_storeInfo:{
                headView.sectionTitle.text = @"";
            } break;
            case StoreDetailSectionType_FeaturedPackages:{//精选套餐
                headView.sectionTitle.text = @"精选套餐";
                headView.sectionContent.text = @"";
                headView.moreButton.hidden = NO;
                WK(weakSelf)
                [headView setClickMoreButton:^{
                    //跳精选套餐
                    PackageListViewController *vc = [[PackageListViewController alloc] init];
                    vc.storeId = weakSelf.storeId;
                    [weakSelf.navigationController pushViewController:vc animated:YES];
                }];
            } break;
            case StoreDetailSectionType_groupPurchasePackage:{//团购套餐
                headView.sectionTitle.text = @"热卖套餐";
                headView.sectionContent.text = @"最热套餐，限时抢购";
            } break;
            case StoreDetailSectionType_merchantWorks:{
                headView.sectionTitle.text = @"商家案例";
                headView.sectionContent.text = @"";
                headView.moreButton.hidden = NO;
                WK(weakSelf)
                [headView setClickMoreButton:^{
                    PortfilioSegmentViewController *vc = [[PortfilioSegmentViewController alloc] init];
                    vc.storeId = weakSelf.storeId;
                    vc.classId = weakSelf.storeDetailModel.seller.classId;
                    [weakSelf.navigationController pushViewController:vc animated:YES];
                }];
            } break;
            case StoreDetailSectionType_businessNews:{
                headView.sectionTitle.text = @"商家动态";
                headView.sectionContent.text = @"";
                headView.moreButton.hidden = NO;
                WK(weakSelf)
                [headView setClickMoreButton:^{
                    //商家动态
                    StoreDynamicListViewController *vc = [[StoreDynamicListViewController alloc] init];
                    vc.storeId = weakSelf.storeId;
                    vc.storeModel = weakSelf.storeDetailModel.seller;
                    [weakSelf.navigationController pushViewController:vc animated:YES];
                }];
            } break;
            case StoreDetailSectionType_userReviews:{
                headView.sectionTitle.text = @"用户评价";
                headView.sectionContent.text = @"查看全部>";
                
                WEAKSELF
                [headView addGestureTapEventHandle:^(id  _Nonnull sender, UITapGestureRecognizer * _Nonnull gestureRecognizer) {
                   
                    EvaluateListViewController *vc = [[EvaluateListViewController alloc] init];
                    vc.storeId = weakSelf.storeId;
                    [weakSelf.navigationController pushViewController:vc animated:YES];
                }];
                
            } break;
            default:
                break;
        }
        
        return  headView;
        
    } else {
                
        StoreDetailEvaluationHeader *headView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"StoreDetailEvaluationHeader"];
        headView.contentView.backgroundColor = UIColor.whiteColor;
        
        if (self.storeDetailModel.userEvaluateList.count > section-6) {
            [headView fillHeadWithModel:self.storeDetailModel.userEvaluateList[section - 6]];
        }
                
        WEAKSELF
        
        [headView.packageBGView addGestureTapEventHandle:^(id  _Nonnull sender, UITapGestureRecognizer * _Nonnull gestureRecognizer) {
           
            if (weakSelf.storeDetailModel.userEvaluateList.count > section-6) {
                
                CommonPackageModel *model = weakSelf.storeDetailModel.userEvaluateList[section - 6].sellerGoods;
                [[TT_JumpManager sharedManager] jumpWithDetailModel:model completion:^(BOOL success, NSString * _Nonnull message) {}];

            }
        }];
        
        [headView addGestureTapEventHandle:^(id  _Nonnull sender, UITapGestureRecognizer * _Nonnull gestureRecognizer) {
           
            if (weakSelf.storeDetailModel.userEvaluateList.count > section-6) {
                EvaluationDetailViewController *vc = [[EvaluationDetailViewController alloc] init];
                vc.ueId = weakSelf.storeDetailModel.userEvaluateList[section - 6].ueId;
                [weakSelf.navigationController pushViewController:vc animated:YES];
            }

        }];
        
        return  headView;
    }

}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WEAKSELF
    if (indexPath.section <=StoreDetailSectionType_userReviews) {
        
        switch (indexPath.section) {
            case StoreDetailSectionType_storeInfo:{

                switch (indexPath.row) {
                    case StoreDetailCellType_storeInfo:{

                        StoreDetailInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"StoreDetailInfoCellID"];
                        cell.selectionStyle =UITableViewCellSelectionStyleNone;
                        return cell;

                    } break;
                    case StoreDetailCellType_storeTag:{

                        StoreDetailTagCell *cell = [tableView dequeueReusableCellWithIdentifier:@"StoreDetailTagCellID"];
                        cell.selectionStyle =UITableViewCellSelectionStyleNone;
                        return cell;
                        
                    } break;
                    case StoreDetailCellType_storeAddress:{

                        StoreDetailAddressCell *cell = [tableView dequeueReusableCellWithIdentifier:@"StoreDetailAddressCellID"];
                        cell.selectionStyle =UITableViewCellSelectionStyleNone;
                        return cell;

                    } break;
                    case StoreDetailCellType_storeCoupon:{

                        StoreDetailCouponCell *cell = [tableView dequeueReusableCellWithIdentifier:@"StoreDetailCouponCellID"];
                        cell.selectionStyle =UITableViewCellSelectionStyleNone;
                        [cell fillCellWithArray:self.storeDetailModel.sysCouponList];
                        return cell;

                    } break;
                    case StoreDetailCellType_storeAnnouncement:{

                        StoreDetailNormalActionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"StoreDetailNormalActionCellID"];
                        cell.selectionStyle =UITableViewCellSelectionStyleNone;
                        
                        cell.mainTitleLab.text = @"公告";
                        cell.subTitleLab.text = self.storeDetailModel.seller.sellerNotice;
                        return cell;

                    } break;
                    case StoreDetailCellType_storeConsultation:{

                        StoreDetailNormalShowCell *cell = [tableView dequeueReusableCellWithIdentifier:@"StoreDetailNormalShowCellID"];
                        cell.selectionStyle =UITableViewCellSelectionStyleNone;
                        cell.mainTitleLab.text = @"咨询礼";
                        cell.contentLab.text = self.storeDetailModel.seller.consultingStar;
                        cell.StoreDetailNormalShowBlock = ^{
                            [[TT_IMContactManager manager] ImContactWithStoreId:weakSelf.storeDetailModel.seller.sellerId];
                        };
                        return cell;
                        
                    } break;
                    case StoreDetailCellType_storeArrival:{

                        StoreDetailNormalShowCell *cell = [tableView dequeueReusableCellWithIdentifier:@"StoreDetailNormalShowCellID"];
                        cell.selectionStyle =UITableViewCellSelectionStyleNone;
                        cell.mainTitleLab.text = @"到店礼";
                        cell.contentLab.text = self.storeDetailModel.seller.arrivalGift;
                        cell.StoreDetailNormalShowBlock = ^{
                            [[TT_IMContactManager manager] ImContactWithStoreId:weakSelf.storeDetailModel.seller.sellerId];
                        };
                        return cell;

                    } break;
                    case StoreDetailCellType_subscribeRing:{
                        
                        StoreDetailRingCell *cell = [tableView dequeueReusableCellWithIdentifier:@"StoreDetailRingCellID"];
                        [cell.mainImageView sd_setImageWithURL:[NSURL URLWithString:self.storeDetailModel.seller.sellerRecommendUrl]];
                        cell.selectionStyle =UITableViewCellSelectionStyleNone;
                        return cell;
                        
                    } break;
                    default:
                        break;
                }

            } break;
            case StoreDetailSectionType_FeaturedPackages:{
                StoreDetailPackageVCell *cell = [tableView dequeueReusableCellWithIdentifier:@"StoreDetailPackageVCellID" forIndexPath:indexPath];
                cell.data = self.storeDetailModel.sellerGoodsList;
                return cell;

            } break;
            case StoreDetailSectionType_groupPurchasePackage:{

                //单个套餐tableviewcell
                StoreDetailPackageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"StoreDetailPackageCellID"];
                cell.selectionStyle =UITableViewCellSelectionStyleNone;
                [cell fillCellWithModel:self.storeDetailModel.sellingSellerGoodsList[indexPath.row]];
                
                cell.StoreDetailPackageLoginBlock = ^{
                  
                    [weakSelf openLoginWithCompletion:^(BOOL isLogin, NSDictionary * _Nonnull options) {
                        if (isLogin) {
                            [weakSelf.tableview reloadData];
                        }
                    }];
                };
                
                return cell;

            } break;
            case StoreDetailSectionType_merchantWorks:{
                switch (indexPath.row) {
                    case 0:{
                        //横向的collectionview
                        StoreDetailOpusCatCell *cell = [tableView dequeueReusableCellWithIdentifier:@"StoreDetailOpusCatCellID"];
                        cell.selectionStyle =UITableViewCellSelectionStyleNone;
                        [cell fillCellWithArray:self.storeDetailModel.sellerOpusClassificationList];
                        cell.StoreDetailOpusCatCellBlock = ^(NSInteger index) {
                            //商家案例
                            PortfilioSegmentViewController *vc = [[PortfilioSegmentViewController alloc] init];
                            vc.storeId = weakSelf.storeId;
                            vc.classId = weakSelf.storeDetailModel.seller.classId;
                            [weakSelf.navigationController pushViewController:vc animated:YES];
                        };
                        return cell;
                    } break;
                    case 1:{
                        //横向的collectionview
                        StoreDetailMerchantWorksCell *cell = [tableView dequeueReusableCellWithIdentifier:@"StoreDetailMerchantWorksCellID"];
                        cell.selectionStyle = UITableViewCellSelectionStyleNone;
                        [cell fillCellWithArray:self.storeDetailModel.sellerOpusList];
                        cell.StoreDetailMerchantWorksCellBlock = ^(NSInteger index) {
                            PortfilioDetailViewController *vc = [[PortfilioDetailViewController alloc] init];
                            vc.soId = weakSelf.storeDetailModel.sellerOpusList[index].soId;
                            [weakSelf.navigationController pushViewController:vc animated:YES];
                        };
                        return cell;
                    } break;
                    default:
                        break;
                }

            } break;
            case StoreDetailSectionType_businessNews:{
                StoreDetailBusinessNewsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"StoreDetailBusinessNewsCellID"];
                cell.selectionStyle =UITableViewCellSelectionStyleNone;
                [cell fillCellWithModel:self.storeDetailModel.dynamicModel];
                return cell;

            } break;
            case StoreDetailSectionType_userReviews:{

                StoreDetailEvaluationCell *cell = [tableView dequeueReusableCellWithIdentifier:@"StoreDetailEvaluationCellID"];
                cell.selectionStyle =UITableViewCellSelectionStyleNone;
                
                return cell;

            } break;
            default:
                break;
        }
        
    } else {
        
        StoreDetailEvaluationCell *cell = [tableView dequeueReusableCellWithIdentifier:@"StoreDetailEvaluationCellID"];
        cell.selectionStyle =UITableViewCellSelectionStyleNone;
        [cell fillCellWithModel:self.storeDetailModel.userEvaluateList[indexPath.section - 6]];

        return cell;
    }

    StoreDetailInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"StoreDetailInfoCellID"];
    cell.selectionStyle =UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    switch (indexPath.section) {
        case StoreDetailSectionType_storeInfo:{

            switch (indexPath.row) {
                case StoreDetailCellType_storeInfo:{} break;
                case StoreDetailCellType_storeVideo:{

                    VideoStorySegmentViewController *vc = [[VideoStorySegmentViewController alloc] init];
                    vc.storeId = self.storeId;
                    vc.model = self.storeDetailModel;
                    vc.selectIndex = 0;
                    [self.navigationController pushViewController:vc animated:YES];
                    
                } break;
                case StoreDetailCellType_storeTag:{} break;
                case StoreDetailCellType_storeAddress:{} break;
                case StoreDetailCellType_storeCoupon:{} break;
                case StoreDetailCellType_storeAnnouncement:{

                    [[BA_BusinessHelper shared] callNoticeWith:@"店铺公告" And:self.storeDetailModel.seller.sellerNotice];
                } break;
                case StoreDetailCellType_storeConsultation:{
                    [[TT_IMContactManager manager] ImContactWithStoreId:self.storeDetailModel.seller.sellerId];
                } break;
                case StoreDetailCellType_storeArrival:{
                    [[TT_IMContactManager manager] ImContactWithStoreId:self.storeDetailModel.seller.sellerId];
                } break;
                case StoreDetailCellType_subscribeRing:{//去领取婚戒
                    RingApplyViewController *vc = [[RingApplyViewController alloc] init];
                    vc.seller = self.storeDetailModel.seller;
                    [self.navigationController pushViewController:vc animated:YES];
                } break;
                default: break;
            }

        } break;
        case StoreDetailSectionType_FeaturedPackages:{//精选套餐cell跳转
        } break;
        case StoreDetailSectionType_groupPurchasePackage:{

            CommonPackageModel *model = self.storeDetailModel.sellingSellerGoodsList[indexPath.row];
            [[TT_JumpManager sharedManager] jumpWithDetailModel:model completion:^(BOOL success, NSString * _Nonnull message) {}];
            
        } break;
        case StoreDetailSectionType_merchantWorks:{

        } break;
        case StoreDetailSectionType_businessNews:{

            StoreDynamicDetailViewController *vc = [[StoreDynamicDetailViewController alloc] init];
            vc.sdyId = self.storeDetailModel.dynamicModel.sdyId;
            [self.navigationController pushViewController:vc animated:YES];
            
        } break;
        case StoreDetailSectionType_userReviews:{

        } break;
        default:
            break;
    }
}

- (UITableView *)tableview {
    if (!_tableview) {
        _tableview = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        _tableview.showsHorizontalScrollIndicator = NO;
        _tableview.showsVerticalScrollIndicator = NO;
        
        _tableview.estimatedSectionHeaderHeight = 0;
        _tableview.estimatedSectionFooterHeight = 0;
        _tableview.estimatedRowHeight = 0;
        _tableview.tag = 10086;
        _tableview.backgroundColor =  UIColorHex(0xf1f1f1);
        _tableview.showsVerticalScrollIndicator = NO;
        
//
        [_tableview registerClass:[StoreDetailOpusCatCell class] forCellReuseIdentifier:@"StoreDetailOpusCatCellID"];
        
        [_tableview registerNib:[UINib nibWithNibName:@"StoreDetailInfoCell" bundle:nil] forCellReuseIdentifier:@"StoreDetailInfoCellID"];
        
        [_tableview registerNib:[UINib nibWithNibName:@"StoreDetailTagCell" bundle:nil] forCellReuseIdentifier:@"StoreDetailTagCellID"];
        
        [_tableview registerNib:[UINib nibWithNibName:@"StoreDetailAddressCell" bundle:nil] forCellReuseIdentifier:@"StoreDetailAddressCellID"];

        [_tableview registerNib:[UINib nibWithNibName:@"StoreDetailCouponCell" bundle:nil] forCellReuseIdentifier:@"StoreDetailCouponCellID"];
        
        [_tableview registerNib:[UINib nibWithNibName:@"StoreDetailNormalActionCell" bundle:nil] forCellReuseIdentifier:@"StoreDetailNormalActionCellID"];

        [_tableview registerNib:[UINib nibWithNibName:@"StoreDetailNormalShowCell" bundle:nil] forCellReuseIdentifier:@"StoreDetailNormalShowCellID"];

        [_tableview registerClass:StoreDetailPackageVCell.class forCellReuseIdentifier:@"StoreDetailPackageVCellID"];

        [_tableview registerNib:[UINib nibWithNibName:@"StoreDetailPackageCell" bundle:nil] forCellReuseIdentifier:@"StoreDetailPackageCellID"];

        [_tableview registerNib:[UINib nibWithNibName:@"StoreDetailRingCell" bundle:nil] forCellReuseIdentifier:@"StoreDetailRingCellID"];
        
        [_tableview registerNib:[UINib nibWithNibName:@"StoreDetailMerchantWorksCell" bundle:nil] forCellReuseIdentifier:@"StoreDetailMerchantWorksCellID"];

        [_tableview registerNib:[UINib nibWithNibName:@"StoreDetailUserWorksCell" bundle:nil] forCellReuseIdentifier:@"StoreDetailUserWorksCellID"];
        
        [_tableview registerNib:[UINib nibWithNibName:@"StoreDetailBusinessNewsCell" bundle:nil] forCellReuseIdentifier:@"StoreDetailBusinessNewsCellID"];
        
        [_tableview registerNib:[UINib nibWithNibName:@"StoreDetailEvaluationCell" bundle:nil] forCellReuseIdentifier:@"StoreDetailEvaluationCellID"];
        
        [_tableview registerNib:[UINib nibWithNibName:@"StoreDetailNormalSectionHeader" bundle:nil] forHeaderFooterViewReuseIdentifier:@"StoreDetailNormalSectionHeader"];
        
        [_tableview registerNib:[UINib nibWithNibName:@"StoreDetailNormalSectionFooter" bundle:nil] forHeaderFooterViewReuseIdentifier:@"StoreDetailNormalSectionFooter"];
        
        [_tableview registerNib:[UINib nibWithNibName:@"StoreDetailEvaluationHeader" bundle:nil] forHeaderFooterViewReuseIdentifier:@"StoreDetailEvaluationHeader"];

        
        
    }
    return _tableview;
}

- (StoreDetailBottomView *)bottomView {
    
    if (!_bottomView) {
        _bottomView =  [[NSBundle mainBundle] loadNibNamed:@"StoreDetailBottomView"owner:nil options:nil].firstObject;
    }
    return _bottomView;
}

- (StoreDetailNaviView *)naviView {
    if (!_naviView) {
        _naviView = [[NSBundle mainBundle] loadNibNamed:@"StoreDetailNaviView"owner:nil options:nil].firstObject;
    }
    return _naviView;
}

- (StoreDetailModel *)storeDetailModel {
    if (!_storeDetailModel) {
        _storeDetailModel = [[StoreDetailModel alloc] init];
    }
    return _storeDetailModel;
}

- (void)request_storeDetail {
    WEAKSELF
    [StoreDetailRequest request_storeDetailWith:self.storeId comption:^(BOOL success, StoreDetailModel * _Nonnull model) {
        if (success) {
            // 隐藏热卖套餐
            model.sellingSellerGoodsList = @[].mutableCopy;
            weakSelf.storeDetailModel = model;
            [weakSelf initView];
            [weakSelf.headerView fillHeaderWithModel:weakSelf.storeDetailModel];
            [weakSelf.tableview reloadData];
            if ([TTUserModel sharedObject].userId.length > 0) {
                [weakSelf addQuickReply];
            }
        }
    }];
}

#pragma mark - 快捷回复模块
- (void)addQuickReply
{
    dispatch_async(dispatch_get_main_queue(), ^{
        //获取快捷回复
        NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
        [parameters setValue:[NSString stringWithFormat:@"%@",self.storeId] forKey:@"sellerId"];
        WK(weakSelf)
        [TTHttpManager postRequestWithUrl:[NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,@"/userProblem/userSelectList"] TimeOut:10 PostData:parameters FinishBlock:^(id  _Nonnull responseObject) {
            //NSLog(@"%@",responseObject);
            TIIMQuickReplyCardModel *responModel = [TIIMQuickReplyCardModel modelWithJSON:responseObject];
            if (responModel.success) {
                if (responModel.data.count > 0) {
                    weakSelf.quickReplyModel = responModel;
                    [weakSelf quickReplyButton];
                }
            } else {
                
            }
        } FailedBlock:^(NSError * _Nonnull error) {
            
        }];
    });
}
- (BaseAdjustButton *)quickReplyButton
{
    if (!_quickReplyButton) {
        BaseAdjustButton *button = [BaseAdjustButton buttonWithType:UIButtonTypeCustom];
        [self.view addSubview:button];
        button.type = layoutTypeHorizontalLeftImageRightTitle;
        button.imageWidth = 10*ADAPTER_WIDTH;
        button.imageHeight = 6*ADAPTER_WIDTH;
        button.title = @"快问";
        button.fontWeight = UIFontWeightMedium;
        button.fontSize = 13*ADAPTER_WIDTH;
        button.marginLeft = 10*ADAPTER_WIDTH;
        button.marginItemX = 4*ADAPTER_WIDTH;
        button.marginRight = 10*ADAPTER_WIDTH;
        button.marginTop = 10*ADAPTER_WIDTH;
        button.marginBottom = 10*ADAPTER_WIDTH;
        [button begin];
        button.right = WIDTH - 16*ADAPTER_WIDTH;
        button.bottom = HEIGHT - 150*ADAPTER_WIDTH - kBottomSafeHeight;
        button.backgroundColor = rgba(0, 0, 0, 0.8);
        button.backgroundColor = rgba(76, 198, 253, 1);
        [button setTitleColor:WhiteColor forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"QuickReply_up"] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"QuickReply_down"] forState:UIControlStateSelected];
        [button setTitle:@"快问" forState:UIControlStateNormal];
        [button setTitle:@"收起" forState:UIControlStateSelected];
        [button addTarget:self action:@selector(clickQuickReplyButton:) forControlEvents:UIControlEventTouchUpInside];
        button.layer.cornerRadius = button.height * 0.5;
        button.layer.masksToBounds = YES;
        _quickReplyButton = button;
    }
    return _quickReplyButton;
}
- (void)clickQuickReplyButton:(UIButton *)sender
{
    sender.selected = !sender.selected;
    if (sender.selected) {
        sender.backgroundColor = rgba(0, 0, 0, 0.8);
        [sender setImage:[UIImage imageNamed:@"QuickReply_dwon"] forState:UIControlStateNormal];
        
        StoreQuickReplyView *view = [[StoreQuickReplyView alloc]initWithFrame:CGRectZero withParent:self withReplyButton:sender];
        view.quickReplyModel = self.quickReplyModel;
        WK(weakSelf)
        [view setCallBackContentBlock:^(NSString * _Nonnull content) {
            [weakSelf gotoChatWithContent:content];
        }];
        self.weakQuickReplyView = view;
    } else {
        sender.backgroundColor = rgba(76, 198, 253, 1);
        [sender setImage:[UIImage imageNamed:@"QuickReply_up"] forState:UIControlStateNormal];
        if (_weakQuickReplyView) {
            [self.weakQuickReplyView dismiss];
        }
    }
}
- (void)gotoChatWithContent:(NSString *)content
{
    NSLog(@"%@",content);
    [[TT_IMContactManager manager] ImContactWithStoreId:self.storeId withQuickReply:content];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    //NSLog(@"scrollViewDidEndDecelerating");
    if (_quickReplyButton) {
        WK(weakSelf)
        [UIView animateWithDuration:0.2 animations:^{
            weakSelf.quickReplyButton.right = WIDTH - 16*ADAPTER_WIDTH;
        }];
    }
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    //NSLog(@"scrollViewDidEndDragging");
    if (_quickReplyButton) {
        WK(weakSelf)
        [UIView animateWithDuration:0.2 animations:^{
            weakSelf.quickReplyButton.right = WIDTH - 16*ADAPTER_WIDTH;
        }];
    }
}
@end
